package net.dgg.liuyinglong.plugin.forms;

import javax.swing.*;
import java.awt.event.*;

public class PluginDialogRefresh extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JCheckBox cb_refresh;
    private JCheckBox cb_loadmore;
    private JTextField et_recview;
    private JTextField et_refresh;
    private JTextField et_mLoadingHelper;
    private JCheckBox cb_err_helper;
    private DialogCallBack dialogCallBack;

    public PluginDialogRefresh(DialogCallBack dialogCallBack) {
        this.dialogCallBack = dialogCallBack;
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        boolean isRefresh = cb_refresh.isSelected();
        boolean isLoadMore = cb_loadmore.isSelected();
        boolean isHelperView = cb_err_helper.isSelected();
        String refresh = et_refresh.getText().trim();
        String recview = et_recview.getText().trim();
        String helperView = et_mLoadingHelper.getText().trim();
        if (null != dialogCallBack) {
            dialogCallBack.ok(isRefresh, isLoadMore, isHelperView, refresh, recview, helperView);
        }
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    public static void main(String[] args) {
        PluginDialogRefresh dialog = new PluginDialogRefresh(new DialogCallBack() {

            @Override
            public void ok(boolean isRefresh, boolean isLoadMore, boolean isHelperView, String refresh, String recview, String helperView) {

            }
        });
        dialog.pack();
        dialog.setVisible(true);
        System.exit(0);
    }

    public interface DialogCallBack {
        void ok(boolean isRefresh, boolean isLoadMore, boolean isHelperView, String refresh, String recview, String helperView);
    }
}
